package com.github.aloxc.plugin.restplus.component;

import com.github.aloxc.plugin.restplus.common.Consts;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.listener.SwaggerFileListener;
import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBPanel;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import java.awt.*;

/**
 * 导入Swagger数据的窗体
 *
 * @author leero
 */
public class SwaggerForm extends JBPanel {
    private JPanel rootPanel;
    public JTextField fileAddress;
    public JPanel SwaggerPanel;
    public JButton fileButton;
    private JLabel chooseLabel;
    private Project project;
    final JLabel progressBar;
    private JFileChooser fileChooser = new JFileChooser();

    @Override
    protected void printComponent(Graphics g) {
        super.printComponent(g);
    }


    public SwaggerForm(Project project) {
        super();
        this.project = project;
        progressBar = new JLabel();
        initComponent();
    }

    private void initComponent() {
        initUi();
        initAction();
    }

    public JTextField getFileAddress() {
        return fileAddress;
    }

    public Project getProject() {
        return project;
    }

    public JLabel getProgressBar() {
        return progressBar;
    }

    public JFileChooser getFileChooser() {
        return fileChooser;
    }

    private void initAction() {
        fileButton.addActionListener(new SwaggerFileListener(this));
    }


    private void initUi() {
        chooseLabel.setText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.swagger.info"));
        fileButton.setText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.swagger.button.text"));
        fileAddress.setAutoscrolls(true);
        SwaggerPanel = new JBPanel();
        GridLayoutManager mgr = new GridLayoutManager(3, 3);
        mgr.setHGap(1);
        mgr.setVGap(0);
        mgr.setMargin(new Insets(20, 20, 20, 20));
        SwaggerPanel.setLayout(mgr);

        Dimension dimension = new Dimension(400, 28);
        progressBar.setForeground(Consts.MAIN_COLOR);
        progressBar.setBackground(Consts.MAIN_COLOR);
        progressBar.setPreferredSize(new Dimension(380, 20));
        progressBar.setVisible(false);
        JPanel contentPane = new JPanel();
        contentPane.add(progressBar,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        dimension, dimension, dimension));
        SwaggerPanel.add(contentPane,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        dimension, dimension, dimension));
        dimension = new Dimension(-1, 28);
        SwaggerPanel.add(chooseLabel,
                new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        dimension, dimension, dimension));
        dimension = new Dimension(-1, 28);

        SwaggerPanel.add(fileAddress,
                new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        dimension, dimension, dimension));
        dimension = new Dimension(50, 28);
        SwaggerPanel.add(fileButton,
                new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_SOUTHEAST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED,
                        dimension, dimension, dimension));
        this.setBorder(BorderFactory.createEmptyBorder());
        this.setLayout(new GridLayoutManager(2, 1));
        this.add(SwaggerPanel,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        null, null, null));
    }
}
